package com.sy.musicapp.ui.home

import androidx.compose.animation.*
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import coil.compose.rememberImagePainter
import com.sy.musicapp.model.Banner
import com.sy.musicapp.ui.theme.Global
import kotlinx.coroutines.delay

/**
 * 轮播图
 * @author SY
 * @since 2022-02-25 20:08
 **/
@ExperimentalAnimationApi
@Composable
fun BannerImg(banners: List<Banner>) {
    var index by remember {
        mutableStateOf(0)
    }
    AnimatedContent(
        targetState = index,
        transitionSpec = {
            fadeIn() with fadeOut()
        }
    ) { targetIndex: Int ->
        if (banners.isNotEmpty()) {
            LaunchedEffect(index) {
                delay(5000)
                index++
                index = if (index == banners.size) 0 else index
            }
            Image(
                painter = rememberImagePainter(data = banners[targetIndex].pic),
                contentDescription = "banner",
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .height(150.dp)
                    .clip(Global.shapes.large)
            )
        }

    }
}